package dk.aau.walkietalkie.network;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

import android.app.Activity;
import android.os.Handler;
import android.util.Base64;
import android.util.Log;
import android.widget.Toast;
import dk.aau.walkietalkieui.IMessageNotifier;
import dk.aau.walkietalkieui.R;
import dk.aau.walkietalkieui.WalkieTalkieApplication;
import dk.aau.walkietalkieui.message.MyMessage;
import dk.aau.walkietalkieui.message.MyMessage.MessageType;

public class TCPClientFileTransfer implements Runnable {
	private static final String TAG = "TCPClientFileTransfer";
	private MyMessage mMsg;

	public TCPClientFileTransfer(MyMessage msg) {
		this.mMsg = msg;
	}

	@Override
	public void run() {
		String Msg_String = " ";

		try {
			Msg_String = msgEncode(mMsg);
			sendEncodedMessage(Msg_String);
			
			Handler mainHandler = new Handler(WalkieTalkieApplication.getContext()
					.getMainLooper());
			Runnable myRunnable = new Runnable() {
				@Override
				public void run() {
					Toast.makeText(
					WalkieTalkieApplication.getContext(),
					R.string.successfully_sent + mMsg.getDataPath() + R.string.to
							+ mMsg.getDestinyName(), Toast.LENGTH_LONG).show();
				}
			};
			mainHandler.post(myRunnable);
			
		} catch (Exception e) {
			e.printStackTrace();
			Log.e(TAG, "Could not encode/send file: " + e.getMessage());
			
			Handler mainHandler = new Handler(WalkieTalkieApplication.getContext()
					.getMainLooper());
			Runnable myRunnable = new Runnable() {
				@Override
				public void run() {
			Toast.makeText(
					WalkieTalkieApplication.getContext(),
					"Sending " + mMsg.getDataPath() + R.string.to
							+ mMsg.getDestinyName() + " FAILED.", Toast.LENGTH_LONG).show();
				}
			};
			mainHandler.post(myRunnable);
		}
	}

	private String msgEncode(MyMessage msg) {

		File file = new File(msg.getDataPath());
		MessageType msgType = msg.getType();

		switch (msgType) {
		case TYPE_RECORD: {
			FileInputStream AudioInFile = null;
			try {
				AudioInFile = new FileInputStream(file);

				byte AudioData[] = new byte[(int) file.length()];

				AudioInFile.read(AudioData);

				String AudioDataString = Base64.encodeToString(AudioData,
						Base64.DEFAULT);
				AudioInFile.close();
				return AudioDataString;
			} catch (Exception e) {
				e.printStackTrace();
			}

			break;
		}
		case TYPE_PICTURE: {

			FileInputStream imageInFile = null;
			try {
				imageInFile = new FileInputStream(file);

				byte imageData[] = new byte[(int) file.length()];

				imageInFile.read(imageData);

				String imageDataString = Base64.encodeToString(imageData,
						Base64.DEFAULT);
				imageInFile.close();
				return imageDataString;
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			/*
			 * ByteArrayOutputStream stream = new ByteArrayOutputStream();
			 * 
			 * byte val [] = stream.toByteArray(); String ba =
			 * Base64.encodeToString(val, Base64.DEFAULT); //return ba;
			 */
			break;
		}
		case TYPE_TEXT: {
			Log.d(TAG, "Envia  TXT caralho!!!!!!!");
			String Txt_string = msg.getDataPath();
			return Txt_string;
		}

		}
		Log.e(TAG, "Null caralho !!!!!");
		return null;

	}

	private void sendEncodedMessage(String encodedMsg) {

		Socket socket = null;
		OutputStream os = null;
		String destination;

		try {
			// destination = "192.168.43.134";
			destination = mMsg.getDestinyIp(); // force this IP
			socket = new Socket(destination, TCPServerFileTransfer.RECEIVER_PORT);
			os = socket.getOutputStream();
			if (os == null) {
				Log.e(TAG, " Could not get output stream of client socket !!!!");
			} else {

				Log.d(TAG, "Connected " + "to " + destination);
				os.write(mMsg.getTCPMessage(encodedMsg).getBytes());
				os.flush();
				os.close();
			}
		} catch (Exception e) {
			String bla = e.getMessage();
			e.printStackTrace();

		} finally {
			try {
				if (socket != null)
					socket.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
}
